class Solution:
    def countSmaller(self, nums: list) -> list:
        def binary(array: list, num: int):
            if len(array) == 0:
                return 0
            left = 0
            right = len(array)
            while left < right:
                mid = (left + right) // 2
                if num <= array[mid]:
                    right = mid
                elif num > array[mid]:
                    left = mid + 1
            return right

        counts = [0 for _ in range(len(nums))]
        array = []
        for i in range(len(nums) - 1, -1, -1):
            index = binary(array, nums[i])
            counts[i] = index
            array.insert(index, nums[i])

        return counts
